/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cassys.action;

import cassys.datatransferobjects.ModuleDTO;
import cassys.datatransferobjects.StudentDTO;
import cassys.datatransferobjects.StudentModuleTeamDTO;
import cassys.datatransferobjects.TeamDTO;
import cassys.delegates.ModuleDelegate;
import cassys.delegates.StudentDelegate;
import cassys.delegates.StudentModuleTeamDelegate;
import cassys.delegates.TeamDelegate;
import java.util.*;

/**
 *
 * @author User
 */
public class StudentModuleAction extends CassysSupport {

    private String studentId;
    private StudentModuleTeamDTO stdMod;
    private String stdModId;
    private String moduleCode;
    private boolean isUpdate = false;
    private List<String> students;
    private ArrayList<StudentModuleTeamDTO> allStdMod;
    private ArrayList<ModuleDTO> modules;
    private ArrayList<StudentDTO> allStudents;
    private ArrayList<TeamDTO> teams;
    private StudentModuleTeamDelegate stdModTeamDelegate = new StudentModuleTeamDelegate();
    private ModuleDelegate mDelegate = new ModuleDelegate();
    private StudentDelegate stdDelegate = new StudentDelegate();
    private TeamDelegate teamDelegate = new TeamDelegate();

    public List<String> getModuleCodes() throws Exception {
        modules = mDelegate.findAllModules();
        List<String> codes = new ArrayList<String>();
        Iterator<ModuleDTO> iter = modules.iterator();
        while (iter.hasNext()) {
            codes.add(iter.next().getModuleCode());
        }
        return codes;
    }

    public ArrayList<StudentDTO> getStudents() throws Exception {

        HashMap<String, StudentDTO> alreadyIn = new HashMap<String, StudentDTO>();
        ArrayList<StudentDTO> out = stdDelegate.findAllStudents();


        /*for (StudentDTO student : allStudents) {
            alreadyIn.put(student.getUserId(), student);
        }

        for (StudentDTO student : out) {
            if (alreadyIn.containsKey(student.getUserId())) {
                out.remove(student);
            }
        }*/
        return out;
    }

    @Override
    public String execute() throws Exception {

        return create();
    }

    public String show() throws Exception {

        allStudents = stdModTeamDelegate.getAllStudentsInModule(moduleCode);

        if (allStudents != null && allStudents.size() > 0) {
            System.out.println("Size :" + allStudents.size());
        } else {
            System.out.println("null");
        }
        return "studentmoduleform";
    }

    public String create() throws Exception {
        isUpdate = false;
        allStudents = stdModTeamDelegate.getAllStudentsInModule(moduleCode);
        return "studentmoduleform";
    }

    public String update() throws Exception {

        StudentModuleTeamDTO tempDTO = new StudentModuleTeamDTO();
        tempDTO.setStudentModuleId("SMT" + getRandomNumber());
        tempDTO.setModulecode(moduleCode);
        tempDTO.setStudentId(getStudentId());
        stdModTeamDelegate.insertStudentModuleTeam(tempDTO);
        return create();
    }

    public boolean isIsUpdate() {
        return isUpdate;
    }

    public void setIsUpdate(boolean isUpdate) {
        this.isUpdate = isUpdate;
    }

    public ArrayList<ModuleDTO> getModules() {
        return modules;
    }

    public void setModules(ArrayList<ModuleDTO> modules) {
        this.modules = modules;
    }

    public StudentModuleTeamDTO getStdMod() {
        return stdMod;
    }

    public void setStdMod(StudentModuleTeamDTO stdMod) {
        this.stdMod = stdMod;
    }

    public String getStdModId() {
        return stdModId;
    }

    public void setStdModId(String stdModId) {
        this.stdModId = stdModId;
    }

    public ArrayList<StudentDTO> getAllStudents() {
        return allStudents;
    }

    public void setAllStudents(ArrayList<StudentDTO> allStudents) {
        this.allStudents = allStudents;
    }

    public ArrayList<TeamDTO> getTeams() {
        return teams;
    }

    public void setTeams(ArrayList<TeamDTO> teams) {
        this.teams = teams;
    }

    public ArrayList<StudentModuleTeamDTO> getAllStdMod() {
        return allStdMod;
    }

    public void setAllStdMod(ArrayList<StudentModuleTeamDTO> allStdMod) {
        this.allStdMod = allStdMod;
    }

    public String getModuleCode() {
        return moduleCode;
    }

    public void setModuleCode(String moduleCode) {
        this.moduleCode = moduleCode;
    }

    protected int getRandomNumber() {
        Random randomGenerator = new Random();
        return randomGenerator.nextInt();
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }
}
